<template>
  <el-dialog class="add-dia social-serve-add" :title="$store.state.dia.add.title" :visible.sync="$store.state.dia.add.show">
    <el-form :model="form">
      <el-form-item label="标题" label-width="80px">
        <el-input v-model="form.title" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="内容" label-width="80px">
        <div id="div3"></div>
      </el-form-item>

      <el-form-item label="附件" label-width="80px">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-remove="remove"
          :on-success="success"
          accept=".doc,.docx,.docm,.dotx,.xls,.ppt"
          multiple
          :limit="3"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$store.state.dia.add.show = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  components: {},
  name: "",
  data() {
    return {
      form:{
        number:"",
        title:"",
        content:"",
        attachment:""
      },
      uploadUrl:PATH+'upload/uploadFile',
      fileList:[],
      editor:null,
      attachment:[],
    };
  },
  methods: {
    save(){
      this.form.content=this.editor.txt.html()
      this.form.table=this.$store.state.dia.add.tableName
      if(this.$store.state.dia.add.id!=="-1")this.form.id=this.$store.state.dia.add.id
      
      if(this.attachment.length>0)this.form.attachment=JSON.stringify(this.attachment)

      if(!this.form.type)this.form.type=this.$store.state.dia.add.type
      // console.log(this.form)
      // return

      this.apiPost(this.$store.state.dia.add.addUrl||"teacher_manage/infoEdit",this.form).then(res=>{
        _g.toastMsg(res.error?"error":"success",res.msg)
        if(!res.error){
          this.$store.state.dia.add.show=false
          bus.$emit("flashSocialTable")
        }
      })
    },

    showEditor(){
      this.$nextTick(()=>{
        var E = window.wangEditor
        this.editor = new E('#div3')
        this.editor.customConfig.uploadImgServer =PATH+'uploadImg'

        this.editor.create()


        if(this.$store.state.dia.add.id>0){
          this.apiPost("teacher_manage/infoDetail",{number:this.form.number,id:this.$store.state.dia.add.id,table:this.$store.state.dia.add.tableName,type:this.$store.state.dia.add.type}).then(res=>{
            this.form.title=res.title
            this.editor.txt.html(res.content)
            this.fileList=JSON.parse(res.attachment||"[]")
          })
        }
      })
    },
    success(response,file,fileList){
      if(!response.error){
        _g.toastMsg("success","文件上传成功")
      }else{
        return
      }
      // console.log(fileList)
      var goal=[]
      fileList.forEach(ele => {
        goal.push({
          name:ele.name,
          url:ele.response?ele.response.msg:ele.url
        })
      });
      this.attachment=goal
    },
    remove(file,fileList){
      var goal=[]
      fileList.forEach(ele => {
        goal.push({
          name:ele.name,
          url:ele.response?ele.response.msg:ele.url
        })
      });
      this.attachment=goal
    }

  },
  created() {
    
  },
  mounted() {
    this.form.number=localStorage.getItem("userNumber")
    this.showEditor()
  },  
};
</script>
<style scoped>
.upload-demo{
  width: 100%;
  padding: 0;
}
</style>